package cn.org.intelli.lims.o2oa.model;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.sub_erupt.Drill;
import xyz.erupt.annotation.sub_erupt.Filter;
import xyz.erupt.annotation.sub_erupt.Link;
import xyz.erupt.annotation.sub_field.*;
import xyz.erupt.annotation.sub_field.sub_edit.ChoiceType;
import xyz.erupt.annotation.sub_field.sub_edit.Search;
import xyz.erupt.annotation.sub_field.sub_edit.VL;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;
import java.util.UUID;

@Getter
@Setter
@Entity
@Table(name = "lims_pcase", uniqueConstraints = {@UniqueConstraint(columnNames = {"ajbh"}), @UniqueConstraint(columnNames = {"slah"})})
@SQLDelete(sql = "update lims_pcase set deleted = true, delete_time = now() where id = ?")
@Erupt(name = "案件信息", dataProxy = Pcase.class, filter = @Filter("Pcase.deleted = false"), drills = {@Drill(title = "案件办理记录", link = @Link(linkErupt = PcaseOp.class, joinColumn = "pcase.id")), @Drill(title = "检材信息", link = @Link(linkErupt = JudicalMaterial.class, joinColumn = "pcase.id")), @Drill(title = "耗材使用记录", link = @Link(linkErupt = MaterialOp.class, joinColumn = "pcase.id")), @Drill(title = "设备使用记录", link = @Link(linkErupt = DeviceOp.class, joinColumn = "pcase.id"))})
public class Pcase implements DataProxy<Pcase> {
    @Id
    @EruptField(views = @View(title = "", type = ViewType.QR_CODE))
    private String id;
    @EruptField(views = @View(title = "案件编号"), edit = @Edit(title = "案件编号", search = @Search))
    private String ajbh;
    @EruptField(views = @View(title = "受理案号"), edit = @Edit(title = "受理案号", search = @Search))
    private String slah;
    @EruptField(views = @View(title = "委托单位"), edit = @Edit(title = "委托单位", search = @Search))
    private String wtdw;
    @EruptField(views = @View(title = "受理日期"), edit = @Edit(title = "受理日期", search = @Search))
    private Date slrq;
    @EruptField(views = @View(title = "委托要求"), edit = @Edit(title = "委托要求"))
    private String wtyq;
    @EruptField(views = @View(title = "简要案情"), edit = @Edit(title = "简要案情"))
    private String jyaq;
    @EruptField(views = @View(title = "案件状态"), edit = @Edit(title = "案件状态"))
    private String status;
    @EruptField(views = @View(title = "流程标识"), edit = @Edit(title = "流程标识", readonly = @Readonly))
    private String o2oaWorkid;
    @ManyToMany
    @JoinTable(name = "lims_pcase_material_desc", joinColumns = @JoinColumn(name = "pcase_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "material_desc_id", referencedColumnName = "id"))
    @EruptField(edit = @Edit(title = "耗材种类信息", type = EditType.TAB_TABLE_REFER))
    private Set<MaterialDesc> materialDescs;
    @ManyToMany
    @JoinTable(name = "lims_pcase_material", joinColumns = @JoinColumn(name = "pcase_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "material_id", referencedColumnName = "id"))
    @EruptField(edit = @Edit(title = "耗材信息", type = EditType.TAB_TABLE_REFER))
    private Set<Material> materials;
    @ManyToMany
    @JoinTable(name = "lims_pcase_device", joinColumns = @JoinColumn(name = "pcase_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "device_id", referencedColumnName = "id"))
    @EruptField(edit = @Edit(title = "设备信息", type = EditType.TAB_TABLE_REFER))
    private Set<Device> devices;
    @Column(name = "delete_time")
    private Date deleteTime;
    private Boolean deleted = false;

    @Override
    public void beforeAdd(Pcase pcase) {
        if (pcase.getId() == null) {
            pcase.setId(UUID.randomUUID().toString());
        }
        pcase.setStatus("待接收");
    }
}
